
<script>
function clickTimKiem()
	{
		if(document.getElementById ('formTimKiem').style.display == 'none')
		{
			document.getElementById ('formTimKiem').style.display = 'block';
		}
		else
		{
			document.getElementById ('formTimKiem').style.display = 'none';
		}
	}
	function word_split($str,$words) {
	$arr = preg_split("/[\s]+/", $str,$words+1);
	$arr = array_slice($arr,0,$words);
	return join(' ',$arr);
 }
</script>

	<div id="menu_tool">
    	<div id="tool_icon">
        	<ul>
            	<li><a href="?go=adduser"><img src="img/add.png" /></a></li>
            </ul>
            <ul>
                <li style="padding:0 5px 0 0px"><a href="?go=adduser">Thêm Mới</a></li>
            </ul>
        </div>
    </div>	
    <?									
				$action = $_REQUEST['action']; 			
					switch($action) {
						case "search":
						{							
							if (!isset($_REQUEST['tennguoi']) && !isset($_REQUEST['trangthai'])) {
								$tennguoi = $_SESSION['tennguoi'];	
								$trangthai = $_SESSION['trangthai'];
								$emailSearch = $_SESSION['emailSearch'];
								$phoneSearch = $_SESSION['phoneSearch'];
								$usernameSearch = $_SESSION['usernameSearch'];
							} else {
								$tennguoi = $_REQUEST['tennguoi'];
								$trangthai = $_REQUEST['trangthai'];
								$emailSearch = $_REQUEST['emailSearch'];
								$phoneSearch = $_REQUEST['phoneSearch'];
								$usernameSearch = $_REQUEST['usernameSearch'];
								$_SESSION['tennguoi'] = $tennguoi;
								$_SESSION['trangthai'] = $trangthai;
								$_SESSION['emailSearch'] = $emailSearch;
								$_SESSION['phoneSearch'] = $phoneSearch;
								$_SESSION['usernameSearch'] = $usernameSearch;
							}
							$sqlSearch = " WHERE idnhomnguoidung != 1 AND tennguoi LIKE '%$tennguoi%' AND username LIKE '%$usernameSearch%' AND email LIKE '%$emailSearch%' AND sodienthoai LIKE '%$phoneSearch%'";
							if ($trangthai < 2) { 
								$sqlSearch .= " AND status = '$trangthai' ";
							}
							$_SESSION["search"] = $sqlSearch;
						}
						break;
						
						case "view":
						{
							$_SESSION["search"] = " WHERE idnhomnguoidung != 1 ";
						}
						break;
						
						case 'add':
						{
 							$tennguoi = $_REQUEST['txtdiachi'];
							$username = $_REQUEST["txthovaten"];
							$status = $_REQUEST['trangthai'];
							$password = $_REQUEST['mk'];
							$encryptPassword = MD5($password);
							$sdt = $_REQUEST['txtdienthoai'];
							$email = $_REQUEST['txtmail'];
							$vaitro = $_REQUEST['vaitro'];
							$sqlCheckUsername = "SELECT username FROM users WHERE username LIKE '$username'";
							$resultCheckUsername =mysql_query($sqlCheckUsername) or die("Can not check username " .mysql_error($conn));							
							if (mysql_num_rows($resultCheckUsername) > 0 ) {
								echo "<script> alert('Username đã tồn tại');</script>";	
								echo "<script> window.history.go(-1); </script>";	
							} else {
								$sqlCheckEmail = "SELECT * FROM users WHERE email LIKE '$email'";
								$resultCheckEmail =mysql_query($sqlCheckEmail) or die("Can not check email " .mysql_error($conn));							
								if (mysql_num_rows($resultCheckEmail) > 0 ) {
									echo "<script> alert('Email đã tồn tại');window.history.go(-1);</script>";
								} else {
									$sql="insert users(username, password, tennguoi, sodienthoai , email, idnhomnguoidung , status) values('$username','$encryptPassword','$tennguoi','$sdt', '$email',$vaitro, $status)";
									$result=mysql_query($sql) or die("Can not insert user " .mysql_error($conn));
									echo "<script> location='?go=user&action=view&page=1';</script>";
								}
							}
						}
						break;
						
						case "del":
						{
								$id = $_REQUEST['id'];
								$sql="select * from users where idusers= $id";
								$result=mysql_query($sql) or die("Can not query delete user".mysql_error($conn));
								$row=mysql_fetch_array($result);
								if(mysql_num_rows($result)!= 0)
								{
									$sql="delete from users where idusers = $id";
									$result=mysql_query($sql) or die("Can not delete user".mysql_error($conn));
								}
								else
								{
									echo "<script> alert('Không thể xóa'); </script>";
								}
								echo "<script>alert('Xóa thành công'); location='?go=user&action=view&page=1';</script>";
						}
						break;
						
						case "edit":
						{
								$id = $_REQUEST['id'];
								$tennguoi = $_REQUEST['txtdiachi'];
								$username = $_REQUEST["txthovaten"];
								$status = $_REQUEST['trangthai'];
								$sdt = $_REQUEST['txtdienthoai'];
								$email = $_REQUEST['txtmail'];
								$vaitro = $_REQUEST['vaitro'];
								$sql="select * from users where username ='$username' and idusers !=$id";
								$result=mysql_query($sql) or die("Can not query user in edit");
								if(mysql_num_rows($result)!=0)
									echo "<script>alert('username đã tồn tại, nhập username khác'); javascript:history.go(-1);</script>";
								else
								{
									$sql="update users set tennguoi ='$tennguoi',status = $status, username = '$username', sodienthoai = '$sdt', email = '$email',idnhomnguoidung = '$vaitro' where idusers =$id";
									$result=mysql_query($sql) or die("Can not update user".mysql_error($conn));
									echo "<script>alert('Sửa thành công');</script>";		
								}
								echo "<script> location='?go=user&action=view&page=1';</script>";
						
						}
						break;
					}
			
		?>
     
        <div id="bang">
    	<table border="1px"  bordercolor="#CCCCCC" cellpadding="0px" cellspacing="0px" cols="8" width="790px">
        		<tr>
				<td colspan="13" align="left"> &nbsp;<input type="button" value="Lọc Dữ Liệu" id="TimKiem" onclick="clickTimKiem()" style="padding:0 5px" /> 
				<form name="formTimKiem" id="formTimKiem" method="post" action="?go=user&action=search&page=1" style="display:none">
				<br />
					<table cellpadding="0" cellspacing="0" align="center" width="100%" border="0">
						<tr>
							<td width="40%" align="right"> Tên người : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="tennguoi"  /> </td>
						</tr>
                        <tr>
							<td width="40%" align="right"> Username : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="usernameSearch"  /> </td>
						</tr>
                        <tr>
							<td width="40%" align="right"> Email : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="emailSearch"  /> </td>
						</tr>
                        <tr>
							<td width="40%" align="right"> Số điện thoại : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="phoneSearch"  /> </td>
						</tr>
						<tr>
							<td align="right"> Trạng thái : &nbsp;</td>
							<td> &nbsp;<select name="trangthai">
								<option value = "2" selected="selected">Tất cả</option>
								<option value="1" >Hoạt động</option>
								<option value="0">Không hoạt động</option>
					
       			 				</select></td>
						</tr>
						<tr>
							<td colspan="2" align="center"> <input type="submit" value="Lọc dữ liệu" style="padding:0 5px" /></td>
						</tr>
					</table>
				<br />
				</form>
				</td>
              
			</tr>
           <!-- <tr>
            	<td colspan="4"></td>
            	<td colspan="2"><input type="button" id="themmoi" value="Thêm Mới" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="kichhoat" value="Kích Hoạt" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="vohieu" value="Vô Hiệu" style="padding: 0 5px"  /></td>                
            </tr>-->
			
            <th style="max-width:30px">STT</th>
            <th style="max-width:100px">Username</th>
            <th style="max-width:150px">Họ Tên </th>
            <th style="max-width:100px">Số Điện Thoại</th>            
            <th style="max-width:150px">Email</th>            
            <th style="max-width:40px">Trạng Thái<th>
            <th style="max-width:50px">Đổi mật khẩu<th>            
            <th style="max-width::30px">Sửa</th>
 			<th style="max-width:30px">Xóa</th>
            <th style="max-width:50px">Kích Hoạt</th>
            <th style="max-width:60px">Vô Hiệu</th>
        <?
			$i = 0;
			$display  = 10;
			$pagenum = $_REQUEST["page"];
			$sql = "SELECT * FROM users";
			$sql .= $_SESSION["search"];
			$queryCount = mysql_query($sql) or die("Can not query count user ".mysql_error($conn));
			$count = mysql_num_rows($queryCount);
			$sql .= " ORDER BY username DESC";
			$result=mysql_query($sql) or die("Can not query user Order".mysql_error($conn));
				$totalpage=ceil(mysql_num_rows($result)/$display);
				if($pagenum<=0 ||$pagenum > $totalpage)
					$pagenum=1;
				$from=($pagenum-1)*$display;
				$sql.="  LIMIT $from, $display";
				$query=mysql_query($sql)  or die("Can not query user Limit".mysql_error($conn));;	
			while ($row=mysql_fetch_array($query)) 
			{			
				$temp = $row['idusers'];
				$i++;
		?>
        	<tr>
            	<td class="stt"><? echo ($from+$i); ?></td>
                <td title="<?=$row['username']?>"><?php echo $row['username'];?></td>
                <td title="<?=$row['tennguoi']?>"><?php echo $row['tennguoi'];?></td>
                <td title="<?=$row['sodienthoai']?>"><?php echo $row['sodienthoai'];?></td>
                <td title="<?=$row['email']?>"><?php echo $row['email'];?></td>                
                <td class="trangthai" align="center"><? if ($row['status'] == 1) {
					echo '<img src="img/Check.jpg"/>';	
				} else {
					echo '<img src="img/Delete.jpg"/>';
				}
				?>
				</td>
            	<td></td> 
                <td align="center"><input type="button" value=" Đổi " onClick="location='?go=changepass&id=<?php echo $temp; ?>';"  style="width:100%"/></td>         <td></td>
                <td class="chucnang" align="center"> <input type="button" value=" Sửa" onClick="location='?go=edituser&id=<?php echo $temp; ?>';"  style="width:100%"/></td>
				<td align="center"> <input type="button" value="Xoá" onClick="if (confirm('Bạn chắc chắn muốn xóa?') == true)
				{
					location='?go=user&action=del&id=<?php echo $temp; ?>';			
				}
				else
				return false;"  style="width:100%"/></td>
        		<td class="chucnang" align="center"> <input type="button" value="Kích Hoạt" onClick="if (confirm('Bạn muốn kích hoạt?') == true) {
                	location='?go=sttuser&action=enable&id=<?php echo $temp; ?>';
                } else {
                	return false;
                }" style="width:90%" /></td>
                <td class="chucnang" align="center"> <input type="button" value="Vô Hiệu" onClick="if (confirm('Bạn muốn vô hiệu hóa?') == true) {
                location='?go=sttuser&action=disable&id=<?php echo $temp; ?>';
                } else return false;"  style="width:90%" /></td>        
            </tr>
        <?		
			} 
			//end while 

			
			?>
             <tr>
				<td colspan="4">Tổng số bản ghi: <? echo $count ?> </td>
                <td colspan="9">Số bản ghi/trang: <? echo $display ?></td>
                </td>
			</tr>
            <tr class="pagelist">
            	<td colspan="13">
                <? 
				for($i=1;$i<=$totalpage;$i++)
				{
					if ($action == 'view') {
					if($i==1)
						echo "<a href='?go=user&action=view&page=$i'>".$i."</a>";
					else
						echo " | <a href='?go=user&action=view&page=$i'>".$i."</a>";
				
				} else {
					if($i==1)
						echo "<a href='?go=user&action=search&page=$i'>".$i."</a>";
					else
						echo " | <a href='?go=user&action=search&page=$i'>".$i."</a>";
				}
				}
                ?>
                </td>
             </tr>

  			
      </table>
    </div>
   
	<div class="clear"></div>
</div>
          
